﻿<%@ WebHandler Language="C#" Class="BookHandlerService" %>

using System;
using System.Web;

using System.Collections;
using System.Collections.Generic;
using System.Reflection;

using XJX.BusinessLayer;

using XJX.Page;

#region 做标记

#endregion

public class BookHandlerService : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";
        
        string response = "";
            
        String methodName = context.Request["method"];
        Type type = this.GetType();
        MethodInfo method = type.GetMethod(methodName);


        if (method == null)
        {
            response = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "调用的方法不存在！").ToJson(); return;
        }

        try
        {
            BeforeInvoke(methodName);
            response=method.Invoke(this, null).ToString();
        }
        catch (Exception ex)
        {
            Hashtable result = new Hashtable();
            result["error"] = -1;
            result["message"] = ex.Message;
            result["stackTrace"] = ex.StackTrace;
            String json = XJX.BusinessLayer.JSON.Encode(result);
            context.Response.Clear();
            context.Response.Write(json);
        }
        finally
        {
            AfterInvoke(methodName);
            HttpContext.Current.Response.Write(response);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    //权限管理
    protected void BeforeInvoke(String methodName)
    {
        //Hashtable user = GetUser();
        //if (user.role == "admin" && methodName == "remove") throw .      
    }
    //日志管理
    protected void AfterInvoke(String methodName)
    {

    }


    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================

    
    //获取一本书的相关信息
    public string Get_One_Book_Info_From_BID()
    {
        string BID = HttpContext.Current.Request["BID"];

        return XJX.Page.Book.Get_One_Book_Info_From_BID(BID);
    }

    //根据分类获取图书
    public string Search_Book_List_From_CategoryID()
    {
        string UID = ((FamilyInfo)HttpContext.Current.Session["FamilyInfo"]).UID;
        
        string result = "";
        string CategoryName = HttpContext.Current.Request["CategoryName"];

        int PageIndex = Convert.ToInt32(HttpContext.Current.Request["PageIndex"]);
        int PageSize = Convert.ToInt32(HttpContext.Current.Request["PageSize"]);

        result = XJX.Page.Book.Search_Book_List_From_CategoryID(CategoryName,UID,PageIndex, PageSize);        
        
        return result;
    }


    //查询图书关键字，获得列表
    public string Search_Book_List_From_BookName()
    {
        string UID = ((FamilyInfo)HttpContext.Current.Session["FamilyInfo"]).UID;
        
        string result = "";
        string BookName = HttpContext.Current.Request["BookName"];

        int PageIndex = Convert.ToInt32(HttpContext.Current.Request["PageIndex"]);
        int PageSize = Convert.ToInt32(HttpContext.Current.Request["PageSize"]);

        result = XJX.Page.Book.Search_Book_List_From_BookName(BookName,UID,PageIndex, PageSize);

        return result;
    }
    
   

    //下架或让架一本书，若ReturnFieldValue为0，则表示上架了，若该值为1，则表示该书目前已经下架了。
    public string Shelf_One_Book_From_BID()
    {
        FamilyInfo fi = (FamilyInfo)HttpContext.Current.Session["FamilyInfo"];

        string UID = fi.UID;
        string BID = HttpContext.Current.Request["BID"];

        int ReturnValue;
        string ReturnFieldValue;
        bool b=XJX.Page.Book.Shelf_One_Book_From_BID(UID, BID, out ReturnValue, out ReturnFieldValue);

        string result="";
        switch (ReturnValue)
        {
            case 1:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "执行成功，当前返回的标志位是："+ReturnFieldValue).ToJson();
                break;
            case 98:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "当前选择的书已经被借出，不能进行此操作!").ToJson();
                break;
            default:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "当前操作出现错误!").ToJson();
                break;
        }
        return result;
    }

    public string Delphi_PostPic()
    {
        string pname = HttpContext.Current.Request["picpath"];

        if (pname.Length > 0)
        {
            string r = "";
            if ((pname.Contains("http://") || pname.Contains("HTTP://") || pname.Contains("https://") || pname.Contains("HTTPS://")))
            {
                r = XJX.Page.Helper.DownFromWebImg(pname);

                if (!string.IsNullOrEmpty(r))
                {
                    pname = r;
                }
                else
                {
                    pname = "1.gif";
                }
            }
            if (pname.Contains("book-default-medium.gif"))
                pname = "1.gif";
        }
        
        return pname;
    }

    //添加新书
    public string Add_New_Book()
    {
        string bname = HttpContext.Current.Request.Form["bookName"];//客户端网页元素必须使用名称表示，不能使用ID表示。例如：name='bname'

        string author = HttpContext.Current.Request.Form["bookAuthor"];
        string press = HttpContext.Current.Request.Form["bookPress"];
        decimal price = Convert.ToDecimal(HttpContext.Current.Request.Form["bookPrice"]);
        string isbn = HttpContext.Current.Request.Form["bookISBN"];
        string pym = HttpContext.Current.Request.Form["bookPYM"];
        string cname = HttpContext.Current.Request.Form["bookCid"];
        string descript = HttpContext.Current.Request.Form["bookDesc"];
        string pname = HttpContext.Current.Request.Form["bookPname"];
        
        FamilyInfo fi = (FamilyInfo)HttpContext.Current.Session["FamilyInfo"];
        string UID = fi.UID;
        
        int ReturnValue = 0;
        string ReturnFieldValue = "";

        if (pname.Length > 0)
        {
            string r = "";
            if ((pname.Contains("http://") || pname.Contains("HTTP://") || pname.Contains("https://") || pname.Contains("HTTPS://")))
            {
                r = XJX.Page.Helper.DownFromWebImg(pname);

                if (!string.IsNullOrEmpty(r))
                {
                    pname = r;
                }
                else
                {
                    pname = "1.gif";
                    //return new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "图书封面图片没有找到，请检查数据!").ToJson();
                }
            }
            if (pname.Contains("book-default-medium.gif"))
                pname = "1.gif";
        }

        //下面开始调用缩略图处理,如果有图片需要上传
        if (HttpContext.Current.Request.Files.Count > 0)
        {
            string fn = "";
            HttpPostedFile file1 = HttpContext.Current.Request.Files["file1"];
            
            //大小判定
            if (file1.ContentLength > 1024 * 1024 * 4)
            {
                return new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "文件不能大于4M").ToJson();
            }
            
            //类型判定
            string imgtype = System.IO.Path.GetExtension(file1.FileName).ToLower();
            if (imgtype != ".jpg" && imgtype != ".jpeg" && imgtype != ".bmp" && imgtype != ".png" && imgtype != ".gif")  //图片类型进行限制
            {
                return new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "请上传bmp\\jpg\\jepg\\png\\gif图片").ToJson();
            }
            
            //图片宽高判定,暂时不需要使用此功能。
            
            
            XJX.Page.Helper.uploadImg(file1,out fn);  //这里引用的是上传封装的方法,返回的是文件名，不含网址路径。

            pname = fn;
        }

        if (pname == null || pname == "")//没有图片上传，也没有图片被指定。
        {
            pname = "1.gif";
        }
        
        //下面开始调用信息保存
        bool b = XJX.Page.Book.Add_New_Book(bname, author, press, price, isbn, pym, cname, descript, pname, UID, out ReturnValue, out ReturnFieldValue);

        string result = "";
        switch (ReturnValue)
        {
            case 1:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "图书添加成功，它的编号是：" + ReturnFieldValue).ToJson();
                break;
            case 99:
                if (pname.Length == 22)
                {
                    System.IO.File.Delete(HttpContext.Current.Server.MapPath("~/upload/pic/" + pname));               
                }
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "添加图书失败，请检查数据!").ToJson();
                break;
            case 100:
                if (pname.Length == 22)
                {
                    System.IO.File.Delete(HttpContext.Current.Server.MapPath("~/upload/pic/" + pname));
                }
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "请不要重复添加同一本书!").ToJson();
                break;                
        }
        return result;
    }
    
    //修改图书信息
    public string Modify_One_Book_Info()
    {
        string BID = HttpContext.Current.Request.Form["BookID"];
        
        string bname = HttpContext.Current.Request.Form["bookName"];
        string author = HttpContext.Current.Request.Form["bookAuthor"];
        string press = HttpContext.Current.Request.Form["bookPress"];
        decimal price = Convert.ToDecimal(HttpContext.Current.Request.Form["bookPrice"]);
        string isbn = HttpContext.Current.Request.Form["bookISBN"];
        string pym = HttpContext.Current.Request.Form["bookPYM"];
        string cname = HttpContext.Current.Request.Form["bookCid"];
        string descript = HttpContext.Current.Request.Form["bookDesc"];
        string pname = HttpContext.Current.Request.Form["bookPname"];
        pname = System.IO.Path.GetFileName(pname);        
        
        FamilyInfo fi = (FamilyInfo)HttpContext.Current.Session["FamilyInfo"];
        string UID = fi.UID;

        int ReturnValue = 0;

        //下面开始调用缩略图处理,如果有图片需要上传
        if (HttpContext.Current.Request.Files.Count > 0)
        {
            string fn = "";
            HttpPostedFile file1 = HttpContext.Current.Request.Files["file1"];

            //大小判定
            if (file1.ContentLength > 1024 * 1024 * 4)
            {
                return new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "文件不能大于4M").ToJson();
            }

            //类型判定
            string imgtype = System.IO.Path.GetExtension(file1.FileName).ToLower();
            if (imgtype != ".jpg" && imgtype != ".jpeg" && imgtype != ".bmp" && imgtype != ".png" && imgtype != ".gif")  //图片类型进行限制
            {
                return new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "请上传bmp\\jpg\\jepg\\png\\gif图片").ToJson();
            }

            //图片宽高判定,暂时不需要使用此功能。


            XJX.Page.Helper.uploadImg(file1, out fn);  //这里引用的是上传封装的方法,返回的是文件名，不含网址路径。

            pname = fn;
        }

        if (pname == null || pname == "")//没有图片上传，也没有图片被指定。
        {
            pname = "1.gif";
        }

        //下面开始调用信息保存
        bool b = XJX.Page.Book.Modify_One_Book_Info(bname, author, press, price, isbn, pym, cname, descript, pname, UID,BID, out ReturnValue);

        string result = "";
        switch (ReturnValue)
        {
            case 1:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "图书信息修改成功！").ToJson();
                break;
            case 98:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "图书已经被借出，信息暂时不能修改!").ToJson();
                break;
            case 99:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "图书信息修改失败，请检查数据!").ToJson();
                break;
        }
        return result;
    }

    //获取某家庭所有图书列表
    public string Get_One_Family_Book_From_UID()
    {
        FamilyInfo fi = (FamilyInfo)HttpContext.Current.Session["FamilyInfo"];
        string UID = fi.UID;

        int PageIndex = Convert.ToInt32(HttpContext.Current.Request["PageIndex"]);
        int PageSize = Convert.ToInt32(HttpContext.Current.Request["PageSize"]);

        return XJX.Page.Book.Get_One_Family_Book_From_UID(UID, PageIndex, PageSize);
    }

    //得到某个家庭的下架书
    public string Get_Off_Shelf_Books_From_UID()
    {
        FamilyInfo fi = (FamilyInfo)HttpContext.Current.Session["FamilyInfo"];
        string UID = fi.UID;

        int PageIndex = Convert.ToInt32(HttpContext.Current.Request["PageIndex"]);
        int PageSize = Convert.ToInt32(HttpContext.Current.Request["PageSize"]);

        return XJX.Page.Book.Get_Off_Shelf_Books_From_UID(UID, PageIndex, PageSize);
    }


    //得到某个家庭的上架书
    public string Get_ON_Shelf_Books_From_UID()
    {
        FamilyInfo fi = (FamilyInfo)HttpContext.Current.Session["FamilyInfo"];
        string UID = fi.UID;

        int PageIndex = Convert.ToInt32(HttpContext.Current.Request["PageIndex"]);
        int PageSize = Convert.ToInt32(HttpContext.Current.Request["PageSize"]);

        return XJX.Page.Book.Get_ON_Shelf_Books_From_UID(UID, PageIndex, PageSize);
    }

    
    
}